import { ref } from "vue";
import { ActionTypeEnum } from "@/enums/actionTypeEnum";
import { getVariantQuickItemList } from "@/api/variantQuickItem/variantQuickItem.api";
import { get } from "lodash-es";

export const useVariantQuickItem = (_bizType: "sales" | "purchase") => {
  const bizType = ref<"sales" | "purchase">(_bizType);
  const actionType = ref<
    Exclude<ActionTypeEnum, ActionTypeEnum.VIEW | ActionTypeEnum.REMOVE>
  >(ActionTypeEnum.ADD);
  const quickIteListLoading = ref<boolean>(false);

  const quickItemList = ref<any[]>([]);

  const handleGetQuickItemList = async () => {
    try {
      quickIteListLoading.value = true;
      const params = {
        type: bizType.value,
        page: 1,
        pageSize: 1000,
      };
      const res = await getVariantQuickItemList(params);
      quickItemList.value = get(res, "result.records") || [];
    } finally {
      quickIteListLoading.value = false;
    }
  };

  const changeActionType = (
    _actionType: Exclude<
      ActionTypeEnum,
      ActionTypeEnum.VIEW | ActionTypeEnum.REMOVE
    >
  ) => {
    actionType.value = _actionType;
  };

  return {
    bizType,
    actionType,
    quickItemList,
    handleGetQuickItemList,
    changeActionType,
  };
};
